#include "config.h"
#include "corefuncs.h"
#include "syncfuncs.h"
#include "fcopy.h"
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <iostream>
#include <string>
#include <cstdio>

using namespace std;

ifstream ignoreList;
bool bIgnore;
string igList;

int initializeSync()
{
	bIgnore = true;
	
	igList = hostDir;
	igList += "/.syncSoft/";
	igList += ignoreListFile;
	
	ignoreList.open(igList.c_str());
	
	
	if( ignoreList.is_open() == false )
		bIgnore = false;
		
	ignoreList.close();
	
	return 1;
}
 
string stripHomeDir(string file)
{	
	return file.substr(strlen(hostDir)+1, file.length()-strlen(hostDir)-1);
}

bool checkIgnoreList( const char *file )
{
	if( !bIgnore )
		return false;
	
	char ignoreLine[MAXDIR];

	if( ignorePlaylistType == PL_M3U )
	{
		ignoreList.open(igList.c_str());
		while( ignoreList.getline( ignoreLine, MAXDIR ) )
		{
			if( !strcmp( ignoreLine, file ) )
				return true;
		}
		ignoreList.close();		
	}

	return false;
}
 
int syncFile(const char *syncFile)
{
	string clientFile = clientDir;
	string hostFile = syncFile;
	clientFile += relativeMusicDir;
	clientFile += "/";
	clientFile += stripHomeDir(syncFile);
	
	if( !fileExists( (char *)clientFile.c_str() ) )
	{
		bool bIsIgnored = false;
		// File Does Not exist on host 
		
		// Check if on ignore list
		if( bIgnore )
		{	
			if( checkIgnoreList( hostFile.c_str() ) )
			{	
				bIsIgnored = true;
			}
		}
		
		if( !bIsIgnored )
		{		
	
			// Either we don't have an ignore list or it's not on there.
			// either way, it needs to be transferred.
			if( FileCopy( hostFile.c_str(), clientFile.c_str() ) == -1 )
			{
				cout << "Error Copying: " << hostFile << endl;
				return 0;
			}
		
			if( bVerbose )
				cout << "Copied: " << clientFile << endl;			
		}
	} else 
	{
		// File exists, but if our user doesn't want it too...
				
		if( bIgnore )
		{
			if( checkIgnoreList( hostFile.c_str() ) )
			{	
				std::remove( clientFile.c_str() );
				if( bVerbose )
					cout << "Removed: " << clientFile << endl;
			}
		}
	}	
	return 1;
}
 
int startSync(char *startDir)
{	
	DIR *dir;
	struct dirent *ent;
	int size = 0;
	static bool start = true;
	
	if( !start )
	{
		string newClientFolder = clientDir;
		newClientFolder += relativeMusicDir;
		newClientFolder += "/";
		newClientFolder += stripHomeDir( startDir );
//		cout << newClientFolder << endl;	
		mkdir( newClientFolder.c_str(), 0777 );
	} else
		start = !start;
	
	
	
	
	//cout << "startDir: " << startDir << endl;
	
	if( (dir = opendir(startDir)) == NULL )
	{
		cout << "Unable to open directory" << endl;
		return 0;
	}
	

	while( (ent = readdir(dir)) != NULL )
	{
		// dont' want to do some reduntant, memory wasting infinite loop
		if( !strcmp( ".", ent->d_name ) || !strcmp( "..", ent->d_name ) || !strcmp( ".syncSoft", ent->d_name ) )
			continue;
	
		DIR *tDir;
		string nDir = startDir;
		nDir += "/";
		nDir += ent->d_name;
		
		if( (tDir = opendir(nDir.c_str())) == NULL )
		{
			syncFile( nDir.c_str() );
		} else {
			// traverse the tree
			startSync( (char *) nDir.c_str() );
		}
		
		
		//cout << ent->d_name << endl;
		size = size+sizeof(ent->d_name)+1;
	}
	
	//cout << "Size is: " << size << endl;
	
	
	if( closedir(dir) != 0 )
	{
		cout << "Unable to close directory!" << endl;
		return 0;
	}
	
}

bool directoryExists( char *Dir )
{
	DIR *dir;
	if( (dir = opendir(Dir)) == NULL )
		return false;
	closedir(dir);
	return true;
	
}

